const user = require('../controllers/user')

const initSocketListener = io => {
  io.sockets.on('connection', socket => {
    console.log(`************new connection************`)
    // 注册
    socket.on('/user/register', async (data, cb) => {
      await user.register(data, socket, cb)
    })
    // 转发请求
    socket.on('/transfer', async (data, cb) => {
      await user.transfer(data, socket, cb)
    })
    // 匹配
    socket.on('/user/match', async (data, cb) => {
      await user.match(data, socket, cb, io)
    })
    // 双人匹配
    socket.on('/user/doubleMatch', async (data, cb) => {
      await user.doubleMatch(data, socket, cb, io)
    })
    // 消息
    socket.on('/user/message', async (data, cb) => {
      await user.message(data, socket, cb, io)
    })
    // 退出房间
    socket.on('/user/leaveRoom', async (data, cb) => {
      await user.leaveRoom(data, socket, cb, io)
    })
    socket.on('/user/startGame', async (data, cb) => {
      await user.startGame(data, socket, cb)
    })
    // 下线
    socket.on('disconnect', async (data, cb) => {
      await user.disconnect(data, socket, cb, io)
    })
  })
}
module.exports = initSocketListener
